package com.mxchip.livestarmobile.mobile.ui.meeting

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.ViewCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.mxchip.livestarmobile.R
import com.mxchip.livestarmobile.mobile.ui.contact.ContactViewModel
import kotlinx.android.synthetic.main.fragment_meeting_invite.*


class MeetingInviteFragment : Fragment() {

    private val contactViewModel by viewModels<ContactViewModel>()

    override fun onDestroyView() {
        super.onDestroyView()
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        val root = inflater.inflate(R.layout.fragment_meeting_invite, container, false)
        ViewCompat.setTranslationZ(root, -1f)
        return root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        recycler_view.layoutManager = LinearLayoutManager(context)
        recycler_view.adapter = MeetingInviteRecyclerAdapter()

        contactViewModel.contactFilterList().observe(viewLifecycleOwner, Observer { list ->

            Log.e("ContactFragment", "list size: " + list.size)

            val adapter = recycler_view.adapter

            if (adapter is MeetingInviteRecyclerAdapter) {
                adapter.data = list
                adapter.notifyDataSetChanged()
            }
        })

        button_hangup.setOnClickListener {

            val intent = Intent()
            val numbers = ArrayList<String>()

            val adapter = recycler_view.adapter

            if (adapter is MeetingInviteRecyclerAdapter) {
                numbers.addAll(adapter.selected.mapNotNull { it.hxbNumber })
            }

            intent.putStringArrayListExtra("numbers", numbers)

            activity?.let {
                it.setResult(Activity.RESULT_OK, intent)
                it.finish()
            }
        }
    }


}